<template>
    <div class="app-container">
        <!-- 查询模块 -->
        <div class="formDiv">
            <el-form ref="form" :inline="true" label-width="140px">
                <el-row>
                    <el-col :span="6">
                        <el-form-item label="姓名">
                            <el-input v-model="form.realName"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="6">
                        <el-form-item label="级别">
                            <el-select placeholder="请选择" v-model="form.roleId">
                                <el-option
                                v-for="item in roleOptions"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value">
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                     <el-col :span="6">
                        <el-form-item label="交钱分公司">
                            <el-select placeholder="请选择" v-model="form.payBranchId">
                                <el-option
                                v-for="item in officeOption"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value">
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="6">
                        <el-form-item label="账号状态">
                            <el-select placeholder="请选择" v-model="form.registerStatus">
                                <el-option
                                v-for="item in options"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value">
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="加入时间">
                            <el-date-picker
                                value-format="yyyy-MM-dd HH:mm:ss"
                                v-model="dates"
                                type="daterange"
                                range-separator="-"
                                start-placeholder="开始日期"
                                end-placeholder="结束日期">
                            </el-date-picker>
                        </el-form-item>
                    </el-col>
                    <el-col :span="6">
                        <el-form-item>
                            <el-button style="margin-left:60px" type="primary" @click="searchFn">查询</el-button>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
        </div>
        <!-- 主页列表 -->
        <div class="listDiv" v-loading="tableLoading">
            <!-- table列表 -->
            <el-table
                :data="tableData"
                class="tableData"
                style="width: 100%">
                <el-table-column
                    prop="realName"
                    label="姓名"
                >
                </el-table-column>
                <el-table-column
                    prop="username"
                    label="手机号"
                >
                </el-table-column>
                <el-table-column
                    prop="roleName"
                    label="级别"
                >
                </el-table-column>
                <el-table-column
                    label="证件类型"
                >
                    <template slot-scope="scope">
                        <span>{{scope.row.cardType | cardNull}}</span>
                    </template>
                </el-table-column>
                <el-table-column
                    prop="card"
                    label="证件号码"
                >
                </el-table-column>
                <el-table-column
                    prop="areaName"
                    label="地区"
                >
                </el-table-column>
                <el-table-column
                    prop="team"
                    label="团队"
                >
                </el-table-column>
                <el-table-column
                    prop="signTime"
                    label="加入时间"
                >
                </el-table-column>
                <el-table-column
                    prop="branchName"
                    label="交钱分公司"
                >
                </el-table-column>
                <el-table-column
                    label="账号类型"
                >
                    <template slot-scope="scope">
                        <span>{{scope.row.type | accountType}}</span>
                    </template>
                </el-table-column>
                <el-table-column
                    label="账号状态"
                >
                    <template slot-scope="scope">
                        <el-switch
                        class="switchStyle"
                        v-model="scope.row.status"
                        @click.native="changeStatus(scope.row.userId,'status',scope.row.status)"
                        active-color="#13ce66"
                        inactive-color="#eee"
                        :active-value="0"
                        :inactive-value="1">
                        </el-switch>
                    </template>
                </el-table-column>
                <el-table-column
                    fixed="right"
                    label="操作"
                    width="100"
                >
                <template slot-scope="scope">
                    <el-button type="text" @click="toRoutes('designerManageEdit',scope.row)" size="small">编辑</el-button>
                    <el-button type="text" @click="toRoutes('designerManageInfo',scope.row)" size="small">详情</el-button>
                    <el-button type="text" @click="toRoutes('designerInfoTeam',scope.row)" size="small">是否团队</el-button>
                </template>
                </el-table-column>
            </el-table>
            <!-- 分页 -->
            <div class="pagesCon">
                <div>
                    <el-pagination
                    @size-change="handleSizeChange"
                    @current-change="handleCurrentChange"
                    :current-page="listForm.pageNum"
                    :page-sizes="page_sizes"
                    :page-size="listForm.limit"
                    layout="total, sizes, prev, pager, next, jumper"
                    :total="page_totals">
                    </el-pagination>
                </div>
            </div>
        </div>
    </div>
</template>
<script>
import { getList,modifyUserServerStatus} from '@/api/designerManage'
import { companyList } from '@/api/branchOfficeDropDown'
export default {
    name:"designerManageIndex",
    data(){
        return {
            officeOption:[],
            tableLoading:true,
            dates:[],
            value1:"",
            options:[{
                    value: '0',
                    label: '正常'
                }, {
                    value: '1',
                    label: '禁用'
                }
            ],
            form:{
                pageNum: 1,
                pageSize:10,
                realName:null,
                roleId:null,
                payBranchId:null,
                signTimeStart:null,
                signTimeEnd:null,
                registerStatus:null
            },
            roleOptions:[],
            tableData: [],
            page_sizes:[10,30,50,100],
            page_totals:60,
            listForm:{
                pageNum: 1,
                pageSize:10
            },
        }
    },
    filters:{
        cardNull(e){
            return window.helper.cardType(String(e))
        },
        accountType(e){
            return window.helper.accountType(String(e))
        }
    },
    mounted(){
        this.getList(this.listForm)
        this.getDrop()
        //获取级别
        this.roleOptions = window.helper.roleFn()
    },
    methods:{
        //上下架与状态修改
        changeStatus(userId,type,status){
            let datas = {
                userId:userId
            }
            datas[type]=status
            modifyUserServerStatus(datas).then(res =>{
                if(res.data.code === 0){
                    this.$message({
                        message: '修改成功',
                        type: 'success'
                    });
                }else{
                    this.$message.error('修改失败');
                }
            })
        },
        //获取相关列表
        getDrop(){
            companyList().then(res =>{
                let resData = res.data.data
                let newData = [];
                resData.forEach(e => {
                    newData.push({
                        value:e.id,
                        label:e.name
                    })
                });
                this.officeOption = newData
            })
            .catch(error =>{
                this.$message.error(error);
            })
        },
        //成功状态弹窗
        succStatus(txt){
            this.$message({
                message: txt+'成功',
                type: 'success'
            });
        },
        //失败状态弹窗
        errorStatus(txt){
            this.$message.error(txt+'失败');
        },
        //查询
        searchFn(){
            if(this.dates.length>0){
                this.form.signTimeStart = this.dates[0]
                this.form.signTimeEnd = this.dates[1]
            }
            this.getList(this.form)
        },
        // 路由跳转
        toRoutes(e,row) {
            let rows = JSON.stringify(row)
            let route = {name:e,query:{userId:rows}}
            this.$router.push(route)
        },
        //列表数据获取
        getList(e){
            this.tableLoading = true
            let datas = e
            getList(datas).then(res => {
                if(!res.data.code){
                    this.tableData = res.data.data.list
                    this.page_totals = res.data.data.count
                }else{
                    this.$message.error("数据接口访问失败");
                }
                this.tableLoading = false
            })
            .catch(error =>{
                this.$message.error(error);
            })
        },
        //删除
        deleteFn(e){
            let datas = {
                id:e
            }
            this.$confirm('删除该条数据, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                deleteData(datas).then(res =>{
                    if(!res.data.code){
                        this.succStatus("删除")
                        this.getList(this.listForm)
                    }else{
                        this.$message.error("数据接口访问失败");
                    }
                })
                .catch(error =>{
                    this.$message.error(error);
                })
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });
        },
        //改变页面条数
        handleSizeChange(val) {
            this.form.pageSize = val
            this.getList(this.form)
        },
        //选择页数
        handleCurrentChange(val) {
            this.form.pageNum = val
            this.getList(this.form)
        },
    }
}
</script>
<style scoped>
/* 分页 */
.pagesCon{height: 40px;margin-top: 20px}
.pagesCon>div{float: right;}
</style>
